import { type PaginationProps } from "@pureadmin/table";
import { addZero } from "@pureadmin/utils";
import { ElMessage, FormInstance } from "element-plus";
import { useRoute } from "vue-router";
import { SpliceDate } from "@/utils/public/method"
import { ref, reactive, nextTick, watch, onMounted, computed } from "vue";
import {
  ProProtectionGetInsure,
  ProProtectionGetInsureDetails,
  ProProtectionFounPremiumCount,
} from "@/api/stu/InsuranceData"
import qs from "qs";
export function useInsuranceDesign() {
  const route = useRoute();
  const columns = [
    {
      label: "保单号",
      prop: "piNumber",
    },
    {
      label: "序号",
      type: "index",
      minWidth: 100
    }, {
      label: "投保人",
      prop: "piApplicant",
    },
    {
      label: "保险产品名称",
      prop: "ppName",
    },
    {
      label: "保险类型",
      prop: "pType",
    },
    {
      label: "申请时间",
      prop: "applyTime",
      cellRenderer: ({ row }) => (
        SpliceDate(row.applyTime)
      )
    }, {
      label: "操作",
      fixed: "right",
      width: 120,
      slot: "operation"
    }

  ];
  const dataList = ref([]);
  const loading = ref(true);
  const btnloading = ref<boolean>(false);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 30,
    currentPage: 1,
    pageSizes: [10, 30, 50, 100],
    background: true
  });
  const DialogVisible = ref(false);
  const car_insurance_item = ref<any>({});
  const sendDate = ref(); //
  const seal = ref(0); //
  // 查看保费计算
  async function ViewDetails(row) {
    const data = qs.stringify({ PIId: row.piId });
    const res = await ProProtectionGetInsureDetails(data)
    if (res.success) {
      DialogVisible.value = true;
      car_insurance_item.value = res.data;
    } else {
      ElMessage.error(res.msg)
    }
  }
  // 提交保费计算
  async function stubim_form() {
    let snum = 0;
    car_insurance_item.value.child.forEach(l => {
      if (!l.premium) snum++
    });
    if (car_insurance_item.value.pType !== '车险') {
      if (!sendDate.value) { ElMessage.warning("请选择发送时间"); return; }
      if (!seal.value) { ElMessage.warning("请盖章"); return; }
    }
    if (snum) { ElMessage.warning("请填写基础保费"); return }
    btnloading.value = true;
    const data = {
      taskId: route.query.taskId,
      planId: route.query.planId,
      piId: car_insurance_item.value.piId,
      ppName: car_insurance_item.value.ppName,
      empPrice: AllPrice.value,
      sendTime: car_insurance_item.value.pType == '车险' ? null : sendDate.value,
      state: car_insurance_item.value.pType == '车险' ? 0 : 1,
      type: car_insurance_item.value.pType,
      detail: car_insurance_item.value.child.map(r => {
        return {
          pdId: r.pdId,
          range: r.ranges,
          projectName: r.projectName,
          price: addZero(r.premium)
        }
      })
    };
    const res = await ProProtectionFounPremiumCount(data);
    if (res.success) {
      ElMessage.success(res.msg);
      btnloading.value = false;
      close_from()
      GetDataList()
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { btnloading.value = false; }, 1500)
    }
  }

  const AllPrice = computed(() => {
    if (!car_insurance_item.value.child) {
      return null;
    }
    let sum = car_insurance_item.value.child.reduce((accumulator, obj) => accumulator + obj.premium, 0);

    try {
      return sum.toFixed(2)
    } catch (err) {
      return '0.00'
    }

  })
  // 关闭弹窗
  function close_from() {
    DialogVisible.value = false;
    car_insurance_item.value = {}
    seal.value = 0;
    sendDate.value = null
  }
  function handleSizeChange() { GetDataList() }
  function handleCurrentChange() { GetDataList() };
  async function GetDataList() {
    const data = {
      "pageIndex": pagination.currentPage,
      "pageCount": pagination.pageSize,
      "taskId": route.query.taskId,
      "planId": route.query.planId,
    }
    const res = await ProProtectionGetInsure(data);
    if (res.success) {
      dataList.value = res.data?.items;
      pagination.total = res.data?.totalCount;
      loading.value = false;
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { loading.value = false; }, 1500)
    }
  }

  onMounted(() => {
    GetDataList();
  })

  return {
    seal,
    AllPrice,
    columns,
    dataList,
    loading,
    sendDate,
    btnloading,
    pagination,
    DialogVisible,
    car_insurance_item,
    close_from,
    stubim_form,
    ViewDetails,
    handleSizeChange,
    handleCurrentChange
  }
}
